<?php 
/*
 * @分销商城
 * @功能说明：PHPExcel
 * @更新说明：暂无更新
 * @文件名 PhpExcel.class.php
 * @编码 UTF-8
 * @创建时间 2015-12-18 下午12:18:33
 * @创建人 XING
 */
namespace service\excel;

/*载入EXCEL文件*/
require SCRIPT_ROOT.'/../../library/phpexcel/PHPExcel.php';
require SCRIPT_ROOT.'/../../library/phpexcel/PHPExcel/Writer/Excel5.php';

class PhpExcel{
	/*
	 * 分销商城_导出EXCEL
	 * @date: 2015-12-18 下午12:23:54
	 * @editor: XING
	 */
	public function loadExcel($fileName='',$keynames = array(),$data = array()){
		$objPHPExcel = new \PHPExcel();
		$xls = array();
		$xls[] = "<html><meta http-equiv=content-type content=\"text/html; charset=UTF-8\"><body><table border='1'>";
		$xls[] = "<tr><td>ID</td><td>" . implode("</td><td>", array_values($keynames)) . '</td></tr>';
		foreach($data As $o) {
			$line = array(++$index);
			foreach($keynames AS $k=>$v) {
				$line[] = $o[$k+1];
			}
			$xls[] = '<tr><td>'. implode("</td><td>", $line) . '</td></tr>';
		}
		$xls[] = '</table></body></html>';
		$xls = join("\r\n", $xls);
		header('Content-Disposition: attachment; filename="'.$fileName.'.xls"');
		die(mb_convert_encoding($xls,'UTF-8','UTF-8'));
	}
	
	/*
	 * 分销商城_Excel
	 * @date: 2016-07-7 12:05:00
	 * @editor: yanyan
	 * */
    public function exportExcel($tableHeader,$data){
        $objPHPExcel = new \PHPExcel();
        //表头数组
//		$tableHeader = array(
//			'订单编号','收货人','手机号码','商品SKUID','商品名称','商品规格','商品数量','省份','城市','详细收货地址','订单成交时间','物流公司','物流运单号','发货仓库','发货时间'
//		);
        //填充表头信息
        $key = ord("A");
        foreach($tableHeader as $value){
            $colum = chr($key);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum.'1',$value);
            $objPHPExcel->getActiveSheet()->getStyle($colum.'1')->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle($colum.'1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
            $objPHPExcel->getActiveSheet()->getColumnDimension($colum)->setWidth(20);
            $key += 1;
        }
        //写入数据
        $colum = 2;
        $objActiveSheet = $objPHPExcel->getActiveSheet();
        foreach($data as $key=>$value){
            $span = ord("A");
            foreach($value as $k=>$v){
                $j = chr($span);
                $objActiveSheet->setCellValue($j.$colum,' '.$v);
                $objPHPExcel->getActiveSheet()->getStyle($j.$colum)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
                $objPHPExcel->getActiveSheet()->getStyle($j.$colum)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
                $span++;
            }
            $colum++;
        }
        $filename = 'qxww'.date('YmdHis').'.xls';
        $objPHPExcel->getActiveSheet()->setTitle('hipay');
        $objPHPExcel->setActiveSheetIndex(0);
        //创建人
        $objPHPExcel->getProperties()->setCreator(getLoginMsg()->username);
        //最后修改人
        $objPHPExcel->getProperties()->setLastModifiedBy(getLoginMsg()->username);
        //标题
        $objPHPExcel->getProperties()->setTitle("嗨呗公主仓库订单");
        //题目
        $objPHPExcel->getProperties()->setSubject("嗨呗公主仓库订单");
        //描述
        $objPHPExcel->getProperties()->setDescription("嗨呗公主仓库订单");
        //关键字
        $objPHPExcel->getProperties()->setKeywords("嗨呗 分销");
        //种类
        $objPHPExcel->getProperties()->setCategory("");
        //设置header信息
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header("Content-Disposition: attachment; filename=\"$filename\"");
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel5');
        $objWriter->save('php://output');
    }

    /*
      导入EXCEL
    *
    **/
    public function importExcel($url){
        $objPHPExcel=\PHPExcel_IOFactory::load($url);
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow();
        $highestColumn = $sheet->getHighestColumn();
        $data = array();
        for($row=2;$row<=$highestRow;$row++){
            for($column="A";$column<=$highestColumn;$column++){
                $myArr = ['A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5,'G'=>6,'H'=>7,'I'=>8,'J'=>9,'K'=>10,'L'=>11,'M'=>12,'N'=>13,'O'=>14,'P'=>15,'Q'=>16,'R'=>17,'S'=>18,'T'=>19,'U'=>20,'V'=>21,'W'=>22,'X'=>23,'Y'=>24,'Z'=>25];
                $addr = $column.$row;
                $header = $sheet->getCell($column.'1')->getValue();
                $cell = $sheet->getCell($addr)->getValue();
                $data[$row-2][$myArr[$column]] = trim($cell);
            }
        }
        return $data;
    }
}

?>